import redis
from django_redis import get_redis_connection


class MyRedis:
    # def __init__(self):
    #     self.conn=get_redis_connection('default')
    # #存储一个字符串
    # def setstr(self,key,value):
    #     self.conn.set(key,value)
    # #删除字符串
    # def delstr(self,key):
    #     self.conn.delete(key)
    # #设置一个过期时间的参数
    # def setex(self,key,value,ex):
    #     self.conn.setex(key,ex,value)
    # #获取到一个字符串
    # def getstr(self,key):
    #     val=self.conn.get(key)
    #     if val:
    #         return val.decode('utf-8')
    #     else:
    #         return None
    #
    # def zset(self,key,value,score):
    #     return self.conn.zincrby(key,score,value)
    #
    # def getzset(self,key):
    #     return self.conn.zrevrange(key,0,-1)
    # 字符串添加
    def __init__(self):
        self.r = redis.Redis(host='127.0.0.1', port=6379, db=1)

    def set_str(self, key, value):
        self.r.set(key, value)

    # 设置时间字符串添加
    def set_time_str(self, key, value, time):
        self.r.setex(key, time, value)

    # 字符串添加
    def get_str(self, key):
        value = self.r.get(key)
        if value:
            return value.decode('utf-8')

    # 删除字符串
    def del_str(self, key):
        return self.r.delete(key)

    # 集合权重
    def set_score(self, key, vaule):
        return self.r.zincrby(key, 1, vaule)

    # 集合获取数据
    def get_set(self, key, len):
        return self.r.zrevrange(key, 0, len)

    def is_exist(self, key):
        return self.r.exists(key)

    def set_time(self, key, time):
        return self.r.expire(key, time)

redistoll=MyRedis()#实例化一个对象